let StuRevisitTable;
let ocTable;
let mainTipPotentialTable;
let trackRemindTable;
let stuOrderInfoTable;
let potentialStuTable;
let mainTipRelLeadsTable;
let newStudentTable;
let ccId = $(".select_item").attr("value");
    //初始化页面框架
    $(function(){
        let configList = selectSort(ccConfig);
        for(let i = 0;i<configList.length;i++){
            if(configList[i].state != 0){
                console.log("configList[i].method=>",configList[i].method)
                 eval(configList[i].method);
            }
        }

        $(".selectbox").click(function(e){
            $(".selectBox").toggle();
            e.stopPropagation();
        });

        $("#selectboxImg").click(function(e){
            $(".selectBox").toggle();
            e.stopPropagation();
        });

        // 点击关闭按钮
        $(".close_icon").click(function(){
            $(this).parents('.main-box').remove()
        });
        //学员属性列表
        getStuStateList();
    });

    //点击排序设置，获取 排序列表
    function sortConfig(){
        var configList =  selectSort(ccConfig);
        //排序列表公共方法
        pubSortList(configList);
    }

    //还原默认设置 方法
    function restoreDefaultConfig(){
        //调用公共方法
       restoreDefault(ccConfig);
    }

    /*约课顾问下潜在学员  客户属性列表*/
    function getStuStateList(){
        $.ajax({
            type : 'post',
            url : '/YkNewMainCtl/revisitInfoList',
            data : {},
            // async:false,
            dataType : 'json',
            success : function(data) {
                let html = "";
                for(let i = 0 ;i<data.revisitInfoList.length;i++){
                    if (data.revisitInfoList[i].parent_id>0){
                        html+='<li style="line-height: 30px; padding-left: 15px;" onclick="selectStuRevisit('+data.revisitInfoList[i].id+',\''+data.revisitInfoList[i].name+'\','+data.revisitInfoList[i].parent_id+')"  value='+data.revisitInfoList[i].id+'>'+data.revisitInfoList[i].name+'</li>';
                    } else {
                        html+='<li style="line-height: 30px;" onclick="selectStuRevisit('+data.revisitInfoList[i].id+',\''+data.revisitInfoList[i].name+'\','+data.revisitInfoList[i].parent_id+')"  value='+data.revisitInfoList[i].id+'>'+data.revisitInfoList[i].name+'</li>';
                    }
                }
                $("#stuRevisit").html(html);
            }
        });
    }

    //点击重新加载 待约学员
    function selectStuRevisit(id,name,parentId){
        let revisitId= $("#appointedStudent").html(name).attr("value");
        $("#appointedStudent").attr("data-parentid",parentId);
        let isReload=true;
        if ((revisitId!=='6' && (id===6 || parentId===6)) || (revisitId==='6' && id!==6 && parentId!==6)) {
            isReload=false;
        }
        if (isReload) {
            mainTipPotentialTable.reload("mainTipPotential", {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    "state" : id
                }
            });
        }else {
            mainTipPotentialTableRender(id,name);
        }
    }
    //创建待约学员表格
    function mainTipPotentialTableRender(id,name) {
        if (id===null || id===""){
            id=6;
        }
        $("#appointedStudent").html(name).attr("value",id);
        let initTableCols=[
            {field:'stuLoginName', title: '学号',fixed: 'left',
                templet : function (row) {
                    return '<a href="javascript:void(0);" onclick="personInfo('+row.id+',\''+row.cnName+'\',14)">'+row.stuLoginName+'</a>';
                }
            },
            {field:'cnName', title: '姓名',fixed: 'left'},
            {field:'channelName', title: '客户渠道',fixed: 'left'},
            {field:'phone', title: '手机'}
        ];
        let parentId=$("#appointedStudent").attr("data-parentid");
        if (id===6 || parentId==="6") {
            initTableCols=[
                {field:'stuLoginName', title: '学号',fixed: 'left',
                    templet : function (row) {
                        return '<a href="javascript:void(0);" onclick="personInfo('+row.id+',\''+row.cnName+'\',14)">'+row.stuLoginName+'</a>';
                    }
                },
                {field:'cnName', title: '姓名',fixed: 'left'},
                {field:'channelName', title: '客户渠道',fixed: 'left'},
                {field:'phone', title: '手机',fixed: 'left'},
                {field:'releaseTime', title: '最近完成的DEMO课',
                    templet : function (row) {
                        //console.log(row.releaseTime);
                        if (row.releaseTime!==null && row.releaseTime!==undefined){
                            return FormatTime(row.releaseTime,1);
                        }
                        return "";
                    }
                }
            ];
        }
        mainTipPotentialTable.render({
            elem: '#5',
            url:'/CCNewMain/appointedStudentByccId',
            method: 'POST', //方式
            height: "400",
            loading: true, //翻页加loading
            where:{"ccId" :  ccId,"state" : id},
            id : "mainTipPotential",
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                groups: 5, //只显示 1 个连续页码
                first: false, //不显示首页
                last: false, //不显示尾页
                theme: '#00A0E9'
            },
            cols: [
                initTableCols
            ]
        });
    }
    //提醒列表 1
    function trackRemindByccId(id,tableName){
        let html = '<span class="date_box">';
         html +='   <a href="javascript:" style="color: #fff" id="trackRemindDate">2019-01-07</a>';
         html +='<i class="fa fa-calendar" style="margin-left: 5px"></i>';
         html +='</span>';
         pubHtml(id,1,tableName,html,"");
    }

    //订单到期列表 2
    function stuOrderInfoByCC(id,tableName){
         pubHtml(id,2,tableName,"","");
    }

    //已约未上 3
    function demoMissedListByCC(id,tableName){
         let html = '<span class="date_box" >';
         html +='   <a href="javascript:" style="color: #fff" id="demoMissed">2019-01-07</a>';
         html +='<i class="fa fa-calendar" style="margin-left: 5px;"></i>';
         html +='</span>';
         pubHtml(id,3,tableName,html,"");

    }
    //潜在客户 4
    function mainTipPotentialByCCId(id,tableName){
        pubHtml(id,2,tableName,"","");
    }

    //待约客户 5
    function appointedStudent(id,tableName){
         let html = '<div class="selectBox" style="border: 1px solid #bdb4b4;">' ;
            html +='   <ul class="list-unstyled" id="stuRevisit">' ;
            html +='   </ul>' ;
            html +='</div>' ;
        let html2 = '<span class="selectbox" id="appointedStudent" style="cursor: pointer;">已上可跟进</span>';
            html2+='<img src="../../static/img/select_icon.png" id="selectboxImg"/>';
        pubHtml(id,3,tableName,html2,html);
    }
    //Ref Leads 6
    function mainTipRelLeadsByCC(id,tableName){
        pubHtml(id,2,tableName,"","");
    }
    //新学员 7
    function mainTipNewStudentByCCId(id,tableName){
        pubHtml(id,3,tableName,"","");
    }
    //oc调查问卷 8
    function ocInfoByCC(id,tableName){
        pubHtml(id,2,tableName,"","");
    }

    //cc 提醒数据表渲染 1-1
    layui.use(['table','laydate'], function(){
          var table = layui.table;
          var laydate = layui.laydate;
          trackRemindTable = table;
          table.render({
            elem: '#1',
            url:'/CCNewMain/selectTrackRemindByccId',
            method: 'POST', //方式
            height: "400",
            loading: true, //翻页加loading
            where:{"ccId" :  ccId},
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
              layout: ['count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
              //,curr: 5 //设定初始在第 5 页
              groups: 5, //只显示 1 个连续页码
              first: false, //不显示首页
              last: false, //不显示尾页
              theme: '#00A0E9'
            },
            cols: [
                [
                  {field:'stuLoginName', title: '学号',fixed: 'left',
                      templet : function (row) {
                         return '<a href="javascript:void(0);" onclick="personInfo('+row.id+',\''+row.cnName+'\',10,'+row.remindId+')">'+row.stuLoginName+'</a>';
                      }},
                  {field:'cnName', title: '姓名',fixed: 'left'},
                  {field: 'releaseTime', title: '提醒时间',
                         templet :function (row){
                            if (row.expired==1){
                                return '<span style="color: #bd2130;">'+FormatTime(row.releaseTime)+'</span>';
                            }else {
                                return '<span style="color: #1abd76;">'+FormatTime(row.releaseTime)+'</span>';
                            }
                         }
                    },
                 {field:'remark', title: '客户属性'}
                ]
            ],
            id : "selectTrackRemind"
        });
        //日期插件  重新加载
        laydate.render({
          elem:"#trackRemindDate",//制定元素
          value: new Date(),
          done:function(value,date){//value, date, endDate点击日期、清空、现在、确定均会触发。回调返回三个参数，分别代表：生成的值、日期时间对象、结束的日期时间对象
             //执行重载
            table.reload("selectTrackRemind", {
                page: {
                  curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    "searchDate" : value
                }
            });
          }
        });
    });

    //提醒列表 reload
    function trackRemindBycc(){
         trackRemindTable.reload("selectTrackRemind", {
            page: {
              curr: 1 //重新从第 1 页开始
            }
         });
    }

    //cc 订单到期列表渲染 2-2
    layui.use('table', function(){
        var table = layui.table;
        stuOrderInfoTable  = table;
        table.render({
            elem: '#2',
            url:'/CCNewMain/searchStuOrderInfoByCC',
            method: 'POST', //方式
            height: "400",
            loading: true, //翻页加loading
            where:{"ccId" :  ccId},
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
              layout: ['count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
              //,curr: 5 //设定初始在第 5 页
              groups: 5, //只显示 1 个连续页码
              first: false, //不显示首页
              last: false, //不显示尾页
              theme: '#00A0E9'
            },
            cols: [
                [
                  {field:'stuLoginName', title: '学号',fixed: 'left',
                      templet : function (row) {
                         return '<a href="javascript:void(0);" onclick="personInfo('+row.id+',\''+row.cnName+'\',11)">'+row.stuLoginName+'</a>';
                      }},
                  {field:'cnName', title: '姓名',fixed: 'left'},
                  {field:'packageName', title: '订单名称'},
                  {field: 'surplusValid', title: '到期日期',
                         templet :function (row){
                            return FormatTime(row.surplusValid,0);
                         }
                    },
                 {field:'remainingCourses', title: '剩余币/课时数'}
                ]
            ],
            id : "stuOrderInfo"
        });
    });

    //订单到期 reload
    function stuOrderInfo(){
        stuOrderInfoTable.reload("stuOrderInfo", {
            page: {
              curr: 1 //重新从第 1 页开始
            }
         });
    }

    //cc 已约未上 3-3
    layui.use(['table','laydate'], function(){
          var table = layui.table;
          var laydate = layui.laydate;
          StuRevisitTable = table;
          table.render({
            elem: '#3',
            url:'/CCNewMain/demoMissedListByCC',
            method: 'POST', //方式
            height: "400",
            loading: true, //翻页加loading
            where:{"ccId" :  ccId},
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
              layout: ['count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
              //,curr: 5 //设定初始在第 5 页
              groups: 5, //只显示 1 个连续页码
              first: false, //不显示首页
              last: false, //不显示尾页
              theme: '#00A0E9'
            },
            cols: [
                [
                {field:'stuLoginName', title: '学号',fixed: 'left',
                      templet : function (row) {
                         return '<a href="javascript:void(0);" onclick="personInfo('+row.id+',\''+row.cnName+'\',12)">'+row.stuLoginName+'</a>';
                      }
                },
                {field:'cnName', title: '姓名',fixed: 'left'},
                {field:'ccName', title: '课程顾问',fixed: 'left'},
                {field: 'surplusValid', title: '上课时间',
                     templet :function (row){
                        return FormatTime(row.releaseTime,1);
                     }
                },
                {field:'teacherName', title: '上课老师',
                      templet : function (row) {
                         return '<a href="javascript:void(0);" onclick="teacherInfo('+row.teacherId+')">'+row.teacherName+'</a>';
                      }
                },
            ]
            ],
            id : "demoMissedList"
        });
        //日期插件  重新加载
        laydate.render({
          elem:"#demoMissed",//制定元素
          value: new Date(),
            trigger: 'click',
          done:function(value,date){//value, date, endDate点击日期、清空、现在、确定均会触发。回调返回三个参数，分别代表：生成的值、日期时间对象、结束的日期时间对象
             //执行重载
            table.reload("demoMissedList", {
                page: {
                  curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    "searchDate" : value
                }
            });
          }
        });
    });



    //已约未上 reload
    function demoMissedList(){
        StuRevisitTable.reload("demoMissedList", {
            page: {
              curr: 1 //重新从第 1 页开始
            }
         });
    }


    //cc 潜在客户 4-4
    layui.use('table', function(){
          var table = layui.table;
          potentialStuTable = table;
          table.render({
            elem: '#4',
            url:'/CCNewMain/selectMainTipPotentialByCCId?state=1',
            method: 'POST', //方式
            height: "400",
            loading: true, //翻页加loading
            where:{"ccId" :  ccId},
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
              layout: ['count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
              //,curr: 5 //设定初始在第 5 页
              groups: 5, //只显示 1 个连续页码
              first: false, //不显示首页
              last: false, //不显示尾页
              theme: '#00A0E9'
            },
            cols: [
                [
                {field:'stuLoginName', title: '学号',fixed: 'left',
                      templet : function (row) {
                         return '<a href="javascript:void(0);" onclick="personInfo('+row.id+',\''+row.cnName+'\',13)">'+row.stuLoginName+'</a>';
                      }
                },
                {field:'cnName', title: '姓名',fixed: 'left'},
                {field:'channelName', title: '客户渠道',fixed: 'left'},
                {field:'phone', title: '手机'}
            ]
            ],
            id : "potentialStu"
        });
    });

    //潜在客户 reload
    function potentialStu(){
        potentialStuTable.reload("potentialStu", {
            page: {
              curr: 1 //重新从第 1 页开始
            },
            where : {
                "state" : 1
            }
         });
    }

    //cc 待约学员 5-5
    layui.use('table', function(){
        let table = layui.table;
        mainTipPotentialTable = table;
        mainTipPotentialTableRender(6,"已上可跟进");
    });

    //待约学员 reload
    function appointedStu(){
        mainTipPotentialTable.reload("mainTipPotential", {
            page: {
              curr: 1 //重新从第 1 页开始
            }
         });
    }

     //Ref leads 6-6
    layui.use('table', function(){
          var table = layui.table;
          mainTipRelLeadsTable = table;
          table.render({
            elem: '#6',
            url:'/CCNewMain/selectMainTipRelLeadsByCC',
            method: 'POST', //方式
            height: "400",
            loading: true, //翻页加loading
            where:{"ccId" :  ccId},
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
              layout: ['count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
              //,curr: 5 //设定初始在第 5 页
              groups: 5, //只显示 1 个连续页码
              first: false, //不显示首页
              last: false, //不显示尾页
              theme: '#00A0E9'
            },
            cols: [
                [
                {field:'stuLoginName', title: '学号',fixed: 'left',
                      templet : function (row) {
                         return '<a href="javascript:void(0);" onclick="personInfo('+row.id+',\''+row.cnName+'\',15)">'+row.stuLoginName+'</a>';
                      }
                },
                {field:'cnName', title: '姓名',fixed: 'left'},
                {field:'channelName', title: '客户渠道',fixed: 'left'},
                {field:'phone', title: '手机'}
            ]
            ],
            id : "mainTipRelLeads"
        });
    });

    //Ref leads reload
    function mainTipRelLeads(){
         mainTipRelLeadsTable.reload("mainTipRelLeads", {
            page: {
              curr: 1 //重新从第 1 页开始
            }
         });
    }

     //新学员 7-7
    layui.use('table', function(){
          var table = layui.table;
          newStudentTable = table;
          table.render({
            elem: '#7',
            url:'/CCNewMain/selectMainTipNewStudentByCCId',
            method: 'POST', //方式
            height: "400",
            loading: true, //翻页加loading
            where:{"ccId" :  ccId},
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
              layout: ['count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
              //,curr: 5 //设定初始在第 5 页
              groups: 5, //只显示 1 个连续页码
              first: false, //不显示首页
              last: false, //不显示尾页
              theme: '#00A0E9'
            },
            cols: [
                [
                {field:'stuLoginName', title: '学号',fixed: 'left',
                      templet : function (row) {
                         return '<a href="javascript:void(0);" onclick="personInfo('+row.id+',\''+row.cnName+'\',16)">'+row.stuLoginName+'</a>';
                      }
                },
                {field:'cnName', title: '姓名',fixed: 'left'},
                {field:'remark', title: '购买天数',fixed: 'left'},
                {field:'phone', title: '手机'}
            ]
            ],
            id : "newStudent"
        });
    });

    //新学员  reload
    function newStudent (){
        newStudentTable.reload("newStudent", {
            page: {
              curr: 1 //重新从第 1 页开始
            }
         });
    }

     //oc 问卷调查 8-8
    layui.use('table', function(){
          var table = layui.table;
          ocTable = table;
          table.render({
            elem: '#8',
            url:'/CCNewMain/selectOcInfoByCC',
            method: 'POST', //方式
            height: "400",
            loading: true, //翻页加loading
            where:{"ccId" :  ccId},
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
              layout: ['count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
              //,curr: 5 //设定初始在第 5 页
              groups: 5, //只显示 1 个连续页码
              first: false, //不显示首页
              last: false, //不显示尾页
              theme: '#00A0E9'
            },
            cols: [
                [
                {field:'stuLoginName', title: '学号',fixed: 'left',
                      templet : function (row) {
                         return '<a href="javascript:void(0);" onclick="personInfo('+row.id+',\''+row.cnName+'\',17)">'+row.stuLoginName+'</a>';
                      }
                },
                {field:'cnName', title: '姓名',fixed: 'left'},
                {field:'phone', title: '手机'},
                {field:'phone', title: '手机',style:"",
                      templet : function (row) {
                        return '<a href="javascript:" class="icon" rel="popover" id="dialing'+row.id+'" onclick="call(\''+row.phone+'\','+row.id+')">' +
                            '    <img src="/static/img/icon/telephone_y.png" class="img-circle" alt="点击可拨打电话" title="点击可拨打电话">' +
                            '  </a>'
                    }
                },
                {field:'state', title: '未回访',
                      templet : function (row) {
                         return '<a url="/questionnaireOc/questionnairePaper?personId='+row.id+'&lessonId='+row.lessonId+'' +'&addUserName=&addUserId='+
                             '&enName='+row.enName+'&cnName='+row.cnName+'&teacher='+row.teacherName+'&releaseTime='+ FormatTime(row.releaseTime,1)+
                             '&sscName='+row.sscName+'&studentLogin='+row.stuLoginName+'" ' +
                             'class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" onclick="ocQuestion(this)" href="javascript:void(0);">未回访</a>';
                      }
                },
            ]
            ],
            id : "OcInfo"
        });
    });

    //oc 问卷 reload
    function ocQuestionReload (){
         ocTable.reload("OcInfo", {
            page: {
              curr: 1 //重新从第 1 页开始
            }
         });
    }

    function ocQuestion(obj) {
       var url = $(obj).attr("url");
       layer.open({
                type: 2,
                title: false,
                closeBtn: 1, //显示关闭按钮
                shade: [0.5],
                area: ['60%', '95%'],
                //offset: 'rb', //右下角弹出
                // time: 2000, //2秒后自动关闭
                anim: 2,
                content: [url, 'yes'], //iframe的url，no代表不显示滚动条
                end: function () { //此处用于演示
                }
        });
    }

    //点击重新加载 待约学员
    function ocReload(){
         StuRevisitTable.reload("OcInfo", {
            page: {
              curr: 1 //重新从第 1 页开始
            }
        });
    }

    /**
     * 接听方式选择
     * @param phone
     * @param id
     * @returns
     */
    function call(phone,id){
        var html = '<div style="font-size: 15px">请选择接听方式</div>';
        html +='<div class="layui-form-item" style=" margin-top: 12px;margin-left: 10px;">' ;
        html += '<button class="layui-btn layui-btn-normal layui-btn-sm" id="saveViod" lay-submit="" lay-filter="demo1" onclick="callOut(\''+phone+'\',1)">手机接听</button>' ;
        html +='<button class="layui-btn layui-btn-warm layui-btn-sm" id="saveViod2" lay-submit="" lay-filter="demo1" onclick="callOut(\''+phone+'\',2)">企话宝接听</button>' ;
        html +='<button style="margin-top: 9%;margin-left: 28%;" type="reset" class="layui-btn layui-btn-primary layui-btn-sm" onclick="layer.closeAll()">稍后再拨</button>' ;
        html += '</div>' ;
        layer.tips(html, '#dialing'+id, {
            time : false,
            tips: [1, '#78BA32']
        });
    }
